﻿using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace LinuxShell
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("------------------本程序由志彬（QQ413377913）提供技术支持------------------");
                using (var sshClient = new SshClient("139.224.186.96", 6382, "root", "Luqiong0816"))
                {
                    Console.WriteLine("正在打开Linux连接");
                    
                    sshClient.Connect();
                    string pid = "";
                    Console.WriteLine("正在执行命令");
                    var rs1 = sshClient.RunCommand("ps -ef|grep tomcat").Result;
                    string[] ls = Regex.Split(rs1, "root", RegexOptions.IgnoreCase);
                    foreach (var item in ls)
                    {
                        string temp = item.Trim();
                        if (temp.IndexOf("tomcat_web1") > -1)
                        {
                            string[] two = temp.Split(' ');
                            if (two.Length > 0)
                            {
                                pid = two[0];
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(pid))
                    {
                        sshClient.RunCommand("kill -9 " + pid);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("系统异常：" + ex.Message);
            }
        }

       

    }
}
